from sqlalchemy import Column, Integer, String, Text, Float, ForeignKey, Enum
from sqlalchemy.orm import relationship
from app.database import Base
import enum


class QuestionType(enum.Enum):
    SINGLE_CHOICE = "single_choice"      # 单选题
    MULTIPLE_CHOICE = "multiple_choice"  # 多选题
    TRUE_FALSE = "true_false"           # 判断题
    FILL_IN_BLANK = "fill_in_blank"     # 填空题
    SHORT_ANSWER = "short_answer"       # 简答题
    ESSAY = "essay"                     # 论述题


class Question(Base):
    __tablename__ = "questions"

    id = Column(Integer, primary_key=True, index=True)
    exam_id = Column(Integer, ForeignKey("exams.id"), nullable=False)
    question_type = Column(Enum(QuestionType), nullable=False)
    title = Column(Text, nullable=False)
    content = Column(Text)  # 题目内容详情
    options = Column(Text)  # 选项（JSON格式存储）
    correct_answer = Column(Text)  # 正确答案
    score = Column(Float, default=1.0)  # 题目分值
    order_num = Column(Integer, default=0)  # 题目顺序
    difficulty = Column(Integer, default=1)  # 难度等级 1-5
    explanation = Column(Text)  # 答案解析
    created_by = Column(Integer, ForeignKey("users.id"), nullable=False)
    
    # 关系
    exam = relationship("Exam", back_populates="questions")
    creator = relationship("User", back_populates="created_questions")
    answers = relationship("Answer", back_populates="question", cascade="all, delete-orphan")